﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYCodeBase.XmlUtility;

namespace HYElectricSetting
{
	public partial class DlgLightning : Form, IElectricSettingItem
	{
		public DlgLightning()
		{
			this.InitializeComponent();
		}

		private void DlgLightning_Load(object sender, EventArgs e)
		{
			this.InitForm();
			this.ReadFormDataFromXml();
			this.isReady = true;
		}

		public bool IsEdited
		{
			get
			{
				return this.isEdited;
			}
			set
			{
				this.isEdited = value;
				HYElectricSettingMainForm hyelectricSettingMainForm = base.Tag as HYElectricSettingMainForm;
				bool flag = this.isReady && this.isEdited && hyelectricSettingMainForm != null;
				if (flag)
				{
					hyelectricSettingMainForm.ValidApplyButton();
				}
			}
		}

		public void SaveElectricSettingData()
		{
			string xmlPath = Path.Combine(Product.UserDataLocation, "Lightning");
			HYCodeBase.XmlUtility.XmlHelper xmlHelper = new HYCodeBase.XmlUtility.XmlHelper(xmlPath, "ElectricData");
			xmlHelper.SelectNode("LGing");
			xmlHelper.SetAttribute("LSpacing", this.txtLSpacing.Text.Trim());
			xmlHelper.SetAttribute("GSpacing", this.txtGSpacing.Text.Trim());
			xmlHelper.SaveXml("");
		}

		private void InitForm()
		{
			this.txtLSpacing.IsNumber = InputSelection.number;
			this.txtLSpacing.IsPositive = SymbolSelect.positive;
			this.txtLSpacing.IsInt = NumberType.integer;
			this.txtGSpacing.IsNumber = InputSelection.number;
			this.txtGSpacing.IsPositive = SymbolSelect.positive;
			this.txtGSpacing.IsInt = NumberType.integer;
		}

		private void ReadFormDataFromXml()
		{
			string xmlPath = Path.Combine(Product.UserDataLocation, "Lightning");
			HYCodeBase.XmlUtility.XmlHelper xmlHelper = new HYCodeBase.XmlUtility.XmlHelper(xmlPath, "ElectricData");
			xmlHelper.SelectNode("LGing");
			this.txtLSpacing.Text = xmlHelper.GetAttributeEx("LSpacing", "1000");
			this.txtGSpacing.Text = xmlHelper.GetAttributeEx("GSpacing", "5000");
		}

		private void txtLSpacing_TextChanged(object sender, EventArgs e)
		{
			this.IsEdited = true;
		}

		private bool isEdited = false;

		private bool isReady = false;
	}
}
